import React, { Component } from 'react';
import { ImageBackground, FlatList, View, Image, RefreshControl, Text, TouchableOpacity, TextInput, StyleSheet } from 'react-native';
import { unitWidth, width } from '../constants/AdapterUtil';
import { MyHeader, MyRightImageHeader } from '../components/MyHeader';
import { api_member_info_bankList, api_member_info_delBank, api_member_info_SubAccountList, api_member_info_updateSubAccount, api_member_address, api_member_address_submit, api_member_address_delete } from '../constants/HttpServices';
import { postFetch } from '../constants/HttpExtension';
import Colors from '../constants/Colors';
import { show } from '../constants/Toast';
import MyListView from '../components/MyListView';
import { CommonModal2 } from '../components/Modal';

export default class AddressManagementScreen extends Component {

    constructor(props) {
        super(props)
        this.state = {
            list: [],
        }
        this.currentChoose = '0'
    }

    componentDidMount() {
        this.props.navigation.addListener('didFocus', payload => {
            this.getData(1);
        })
    }

    render() {

        return (
            <View style={{ flex: 1, backgroundColor: Colors.colorF5 }}>
                <MyRightImageHeader title='我的收货地址' leftImg={require('../res/ic_arrow_left.png')} leftClick={() => { this.props.navigation.pop() }}
                    rightImg={require('../res/me_grxx.png')} rightClick={() => {
                        this.props.navigation.navigate('MeAddAddress')
                    }} />
                <MyListView
                    ref='MyListView'
                    list={this.state.list}
                    EmptyView={
                        <View style={{ height: 450 * unitWidth, alignItems: 'center', marginTop: 70 * unitWidth }}>
                            <Image style={{
                                width: 328 * unitWidth,
                                height: 241 * unitWidth, marginTop: 40 * unitWidth
                            }} source={require('../res/me_shdzkzt.png')} resizeMode='stretch' />
                            <Text style={{ fontSize: 17 }}>您还未添加收货地址，<Text style={{ color: Colors.colorOrange }} onPress={() => {
                                this.props.navigation.navigate('MeAddAddress')
                            }}>现在去添加</Text></Text>
                        </View>
                    }
                    renderItem={this.getItem.bind(this)}
                    getList={this.getData.bind(this)}
                />
                <CommonModal2 ref='CommonModal2' title='是否删除该收货地址？' comfirmText='确定' cancelText='取消'
                    cancelClick={() => {

                    }}
                    comfirmClick={() => {
                        this.delete()
                    }} />
            </View>
        );
    }

    getItem({ item, index }) {
        return (
            <View style={{ height: 123 * unitWidth, backgroundColor: Colors.white, marginTop: 5 * unitWidth, paddingHorizontal: 25 * unitWidth, paddingTop: 10 * unitWidth }}>
                <Text style={{ fontSize: 14, color: Colors.black }}>{'收件人:  ' + item.realname + '   ' + item.mobile}</Text>
                <Text style={{ fontSize: 14, color: Colors.black, marginTop: 10 * unitWidth }}>{'详细地址：  ' + item.address}</Text>
                <View style={{ backgroundColor: Colors.colorEF, width: 340 * unitWidth, alignSelf: 'center', height: 0.5, marginTop: 17 * unitWidth }} />
                <View style={{ flex: 1, flexDirection: 'row', alignItems: 'center', justifyContent: 'space-between' }}>
                    <TouchableOpacity style={{ flexDirection: 'row', alignItems: 'center' }} activeOpacity={0.7}
                        onPress={() => {
                            this.update(item)
                        }}>
                        <Image style={{
                            width: 16 * unitWidth,
                            height: 16 * unitWidth
                        }} source={item.isdefault == '1' ? require('../res/grzx_dzglxz2.png') : require('../res/grzx_dzglxz1.png')} resizeMode='stretch' />
                        <Text style={{
                            fontSize: 14, marginLeft: 10 * unitWidth,
                            color: item.isdefault == '1' ? Colors.colorOrange : Colors.black
                        }}>设为默认地址</Text>
                    </TouchableOpacity>
                    <View style={{ flexDirection: 'row', alignItems: 'center' }}>
                        <TouchableOpacity style={{ flexDirection: 'row', alignItems: 'center' }} activeOpacity={0.7}
                            onPress={() => {
                                this.props.navigation.navigate('MeAddAddress', {
                                    id: item.id,
                                    data: {
                                        mobile: item.mobile,
                                        name: item.realname,
                                        province: item.province,
                                        city: item.city,
                                        area: item.area,
                                        address: item.address,
                                    }
                                })
                            }}>
                            <Image style={{
                                width: 16 * unitWidth,
                                height: 16 * unitWidth
                            }} source={require('../res/sc_shdzxg.png')} resizeMode='stretch' />
                            <Text style={{
                                fontSize: 14, marginLeft: 7 * unitWidth,
                                color: Colors.color99
                            }}>修改</Text>
                        </TouchableOpacity>
                        <TouchableOpacity style={{ flexDirection: 'row', alignItems: 'center', marginLeft: 15 * unitWidth }} activeOpacity={0.7}
                            onPress={() => {
                                this.currentChoose = item.id
                                this.refs.CommonModal2.showModal();
                            }}>
                            <Image style={{
                                width: 16 * unitWidth,
                                height: 16 * unitWidth
                            }} source={require('../res/sc_shdzsc.png')} resizeMode='stretch' />
                            <Text style={{
                                fontSize: 14, marginLeft: 7 * unitWidth,
                                color: Colors.color99
                            }}>删除</Text>
                        </TouchableOpacity>
                    </View>
                </View>
            </View>
        )
    }

    //获取
    getData(page) {
        let params = {

        }
        postFetch(api_member_address, params, {
            success: (response) => {
                if (page == 1) {
                    this.refs.MyListView.onHasMore();
                    this.setState({
                        list: response.result.list,
                    })
                } else {
                    if (response.result.list && response.result.list.length > 0) {
                        this.refs.MyListView.onHasMore();
                        this.setState({
                            list: this.state.list.concat(response.result.list),
                        })
                    } else {
                        this.refs.MyListView.onNoMore();
                    }
                }
            },
            fail: (response) => {
                this.refs.MyListView.onNoMore();
                show(response.result.message);
            }
        })
    }

    update(item) {
        let params = {
            'id': item.id,
            "realname": item.realname,
            "mobile": item.mobile,
            "address": item.address,
            "areas": item.areas,
            "isdefault": item.isdefault == '1' ? '0' : '1',
        }
        postFetch(api_member_address_submit, params, {
            success: (response) => {
                this.refs.MyListView.onRefresh();
            },
            fail: (response) => {
                show(response.result.message);
            }
        })
    }

    delete() {
        let params = {
            'id': this.currentChoose,
        }
        postFetch(api_member_address_delete, params, {
            success: (response) => {
                this.refs.MyListView.onRefresh();
            },
            fail: (response) => {
                show(response.result.message);
            }
        })
    }
}